package org.wenhuayuan.uilibrary.banner.core;

import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.List;

public class HiBannerAdapter extends PagerAdapter  {

    private Context context;
    private SparseArray<HiBannerViewHolder> cacheViews = new SparseArray<>();
    private IBindAdapter iBindAdapter;
    private IHiBanner.OnBannerClickListener onBannerClickListener;
    private List<? extends HiBannerMo> models;

    private ViewPager viewPager;

    private boolean autoPlay = false; // 是否开启自动轮播
    private boolean loop = false; // 是否开启循环播放
    private int layoutResId = -1;

    public HiBannerAdapter(Context context) {
        this.context = context;
    }

    public void setBannerData(@NonNull List<? extends HiBannerMo> models, ViewPager viewPager) {
        this.models = models;
        this.viewPager = viewPager;
        // 初始化数据
        initCachedView();
        // 然后通知刷新
        notifyDataSetChanged();
    }

    public void setBindAdapter(IBindAdapter iBindAdapter) {
        this.iBindAdapter = iBindAdapter;
    }

    public void setOnBannerClickListener(IHiBanner.OnBannerClickListener onBannerClickListener) {
        this.onBannerClickListener = onBannerClickListener;
    }

    public void setAutoPlay(boolean autoPlay) {
        this.autoPlay = autoPlay;
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    public void setLayoutResId(@NonNull int layoutResId) {
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        // 这里是，实现无线轮播的关键
        return autoPlay ? Integer.MAX_VALUE : (loop ? Integer.MAX_VALUE : getRealCount());
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
    public int getFirstItem() {
        return Integer.MAX_VALUE/2 - (Integer.MAX_VALUE/2) % getRealCount();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        // 让item每次都会刷新 这样inflate的时候，能及时banner数据
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        // 防止instantiateItem执行初始化时，缓存中的view被销毁
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        int realPosition = position;
        if (getRealCount() > 0 ){
            realPosition = realPosition % getRealCount();
        }
        HiBannerViewHolder viewHolder = cacheViews.get(realPosition);
        if(container.equals(viewHolder.rootView.getParent())) {
            container.removeView(viewHolder.rootView);
        }
        // 数据绑定
        onBind(viewHolder, models.get(realPosition), realPosition);
        if (viewHolder.rootView.getParent() != null) {
            ((ViewGroup)viewHolder.rootView.getParent()).removeView(viewHolder.rootView);
        }
        container.addView(viewHolder.rootView);
        return viewHolder.rootView;
    }
    // 数据绑定
    public void onBind(@NonNull final HiBannerViewHolder viewHolder, @NonNull final HiBannerMo mo, final int position){
        viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBannerClickListener != null) {
                    onBannerClickListener.onBannerClick(viewHolder, mo, position);
                }
            }
        });
        viewHolder.rootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onBannerClickListener != null) {
                    if(viewPager != null) {
//                        ((HiViewPager)viewPager).stop();
                    }
                    onBannerClickListener.onBannerLongClick(viewHolder, mo, position);
                }
                return true;
            }
        });
        Log.i("HiBannerAdapter", "iBindAdapter = " + iBindAdapter);
        if (null != iBindAdapter) {
            // 业务层进行数据的绑定
            iBindAdapter.onBind(viewHolder, mo, position);
        }
    }

    public int getRealCount () {
        return models == null ? 0 : models.size();
    }

    private void initCachedView() {
        cacheViews = new SparseArray<>();
        // 遍历所有的models
        for (int i = 0; i < models.size(); i ++) {
            HiBannerViewHolder viewHolder = new HiBannerViewHolder(createRootView(LayoutInflater.from(context), null));
            cacheViews.put(i, viewHolder);
        }
    }

    private View createRootView(LayoutInflater inflater, ViewGroup parent) {
        if(layoutResId == -1) {
            throw new IllegalArgumentException("you must be set layoutResId first");
        }

        return inflater.inflate(layoutResId, parent,false);
    }

    public static class HiBannerViewHolder {
        View rootView;
        SparseArray<View> arrayView;
        public HiBannerViewHolder(View rootView) {
            this.rootView = rootView;
        }

        public View getRootView() {
            return rootView;
        }

        public <V extends View>V findViewById(int id) {
            if(!(rootView instanceof ViewGroup)) return (V) rootView;
            if(arrayView == null) {
                arrayView = new SparseArray<>();
            }
            V child = (V) arrayView.get(id);
            if (null == child) {
                child = rootView.findViewById(id);
                arrayView.put(id, child);
            }
            return child;
        }
    }
}
